package com.shop.service.impl;

import com.shop.dao.IAddressDao;
import com.shop.dao.impl.AddressDaoImpl;
import com.shop.entity.Address;
import com.shop.service.IAddressService;

import java.util.List;

public class AddressServiceImpl implements IAddressService {

    private IAddressDao addressDao = new AddressDaoImpl();

    /**
     * 通过uid查询所有收货地址
     * @param uid
     * @return
     */
    @Override
    public List<Address> findAddressByUid(int uid) {
        return addressDao.findAddressByUid(uid);
    }

    /**
     * 更新地址
     * @param address
     * @return
     */
    @Override
    public int update(Address address) {
        return addressDao.update(address);
    }

    /**
     * 添加地址
     * @param address
     * @return
     */
    @Override
    public int add(Address address) {
        List<Address> addressList = addressDao.findAddressByUid(address.getUid());
        if (addressList.size()==0){
            address.setAstate(1);
        }
        return addressDao.add(address);
    }

    /**
     * 删除地址
     * @param aid
     * @return
     */
    @Override
    public int delete(int aid) {
        return addressDao.delete(aid);
    }

    /**
     * 设置默认地址
     * @param aid
     * @return
     */
    @Override
    public int setDefault(int aid,int uid) {
        List<Address> address = addressDao.findAddressByUid(uid);
        for (Address a:address){
            if (a.getAstate()==1){
                addressDao.setDefault(a.getAid(),0);
            }
        }
        return addressDao.setDefault(aid,1);
    }
}
